package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.WumingErrorCode;
import com.wuming.house.dao.BannerMapper;
import com.wuming.house.model.Banner;
import com.wuming.house.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("bannerService")
public class BannerServiceImpl extends BaseServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public void saveBanner(Banner banner) throws BusinessException {
        bannerMapper.insert(banner);
    }

    @Override
    public void modifyBanner(Banner banner) throws BusinessException {
        bannerMapper.updateByPrimaryKey(banner);
    }

    @Override
    public void removeBannerById(Long id) throws BusinessException {
        bannerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Banner getBannerById(Long id) throws BusinessException {
        return bannerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Banner> getAllBannerByType(Integer type) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);
        return bannerMapper.selectByParams(params);
    }

    @Override
    public List<Banner> getBannerByPage(int pageIndex, int pageSize) throws BusinessException {
        try {
            return (List<Banner>) this.getPageList(BannerMapper.class, "selectByParams", null, pageIndex, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal() throws BusinessException {
        try {
            return this.getTotal(BannerMapper.class, "selectTotal", null);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }
}
